﻿using System;
using System.Collections.ObjectModel;
using Microsoft.ApplicationServer.Caching;
using System.Threading.Tasks;

namespace MDCAdminTool
{
    public class CacheRegion : CacheEntityBase
    {
        #region Properties
        public string Role { get; set; }
        public int PortNo { get; set; }
        public bool EvictionOn { get; set; }
        public CacheRegionStats CacheRegionStats { get; set; }
        public Collection<DataCacheItem> CacheItems { get; set; }
        public int CacheItemsCount
        {
            get
            {
                if (CacheItems == null)
                    return 0;
                
                return CacheItems.Count;
            }
        }
        #endregion

        public CacheRegion(string region, string role , int port, MdcCache cache) : this( region , false , cache)
        {
            Role   = role;
            PortNo = port;
        }
        public CacheRegion( string region , bool evictionOn , MdcCache cache )
        {
            Name       = region;
            Parent     = cache;
            EvictionOn = evictionOn;
            Output     = cache.Output;
            CacheItems = new Collection<DataCacheItem>();

            new TaskFactory()
                .StartNew(LoadChildren);

        }

        public override string ToString()
        {
            return Name;
        }
        public override void LoadChildren()
        {
            try
            {
                Log("DataCliet.GetObjectsInRegion(" + Name + ")");

                var cache = Parent as MdcCache;

                var dc = CacheClientFactory.GetCache( cache );
                if (dc == null) return;

                var r = dc.GetObjectsInRegion(Name);
                var result = new Collection<DataCacheItem>();
                if (r != null)
                {
                    Parallel.ForEach( r , 
                        (item)=>
                        {
                            DataCacheItem di;
                            if (Name.Contains("Default_Region"))
                            {
                                di = dc.GetCacheItem(item.Key);
                            }
                            else
                            {
                                di = dc.GetCacheItem(item.Key , Name);
                            }
                            if (di != null)
                                result.Add(di);
                            
                        }
                    );
                    CacheItems = result;
                }
            }
            catch (Exception ex)
            {
                Log(new PowerShellCommand { CommandName = ex.Message });
            }
            
        }

        #region ICacheCommand Not Implemented
        public override void Start()
        {
            throw new NotImplementedException();
        }
        public override void Stop()
        {
            throw new NotImplementedException();
        }

        public override void Add(CacheEntityBase item)
        {
            throw new NotImplementedException();
        }
        public override void Remove( CacheEntityBase item )
        {
            throw new NotImplementedException();
        }

        public override CacheStats LoadStatistics()
        {
            throw new NotImplementedException();
        }
        public override void LoadConfig()
        {
            throw new NotImplementedException();
        }
        public override void SaveConfig()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
